package com.taptech.doufu.ui.view.personalcenter;

import java.util.ArrayList;
import java.util.List;

import com.taptech.doufu.util.ScreenUtil2;
import com.taptech.doufu.util.TTLog;
import com.taptech.doufu.R;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.util.at.MyClickListenner;

import com.taptech.doufu.util.TagsUtil;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.view.View.OnClickListener;

/**
 * Created by LongSiyang on 2015/5/16.
 */

public class TextTagsViewGroup extends TagsViewGroup implements OnClickListener {


    public final static int MESSAGE_TAG_CLICK = 10001;
    public final static int MESSAGE_TAG_DELTE = 10002;
    protected int mTextSize = 14;

    protected int mPaddingWidth = 0;
    protected int mPaddingHeight = 0;
    protected int mTextColor = Color.parseColor("#ff000000");
    protected int mTextWidth = ViewGroup.LayoutParams.WRAP_CONTENT;
    protected int mTextHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    // 豆花标签背景色
    public MyClickListenner tagClickListener;


    public TextTagsViewGroup(Context context) {
        super(context);
    }

    public TextTagsViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextTagsViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }



    public int addDoufuTagViewForTag(Context context, String text) {

        View view = LayoutInflater.from(mContext).inflate(R.layout.tags_create_view_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.tag_text);
        ImageView deleteImage = (ImageView) view.findViewById(R.id.tag_delete_image);
        deleteImage.setOnClickListener(this);
        textView.setOnClickListener(this);

        view.setPadding(mPaddingWidth, mPaddingHeight, mPaddingWidth, mPaddingHeight);
        textView.setTextSize(mTextSize);
        textView.setTextColor(Color.parseColor("#5d5d5d"));
        textView.setBackgroundResource(R.drawable.tag_bgc);
        textView.setText(text);
        addView(view);
        return getChildCount();
    }




    public int getmTextSize() {
        return mTextSize;
    }

    public void setmTextSize(int mTextSize) {
        this.mTextSize = mTextSize;
    }

    public int getmTextColor() {
        return mTextColor;
    }

    public void setmTextColor(int mTextColor) {
        this.mTextColor = mTextColor;
    }

    public int getmTextWidth() {
        return mTextWidth;
    }

    public void setmTextWidth(int mTextWidth) {
        this.mTextWidth = mTextWidth;
    }

    public int getmTextHeight() {
        return mTextHeight;
    }

    public void setmTextHeight(int mTextHeight) {
        this.mTextHeight = mTextHeight;
    }

    public int getmPaddingWidth() {
        return mPaddingWidth;
    }

    public void setmPaddingWidth(int mPaddingWidth) {
        this.mPaddingWidth = mPaddingWidth;
    }

    public int getmPaddingHeight() {
        return mPaddingHeight;
    }

    public void setmPaddingHeight(int mPaddingHeight) {
        this.mPaddingHeight = mPaddingHeight;
    }

    @Override

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tag_delete_image:
                View tempview = (View) view.getParent();
                tempview.setClickable(false);
                removeView(tempview);
                String[] tagsStrings = this.getStrings();

                if (tagClickListener == null) {
                    PersonalInfoService.getInstance().saveUserTagsInfo(null, tagsStrings);
                } else {
                    // String tagString = ((TextView) view).getText().toString();
                    TextView textView = (TextView) tempview.findViewById(R.id.tag_text);
                    String tagString = textView.getText().toString();
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("tagString", tagString);
                    message.setData(bundle);
                    message.what = MESSAGE_TAG_DELTE;
                    tagClickListener.handler.sendMessage(message);
                }
                break;
            case R.id.tag_text:
                // tagClickListener.onClick(view);

                if (tagClickListener != null) {
                    String tagString = ((TextView) view).getText().toString();
                    Message message = new Message();
                    Bundle bundle = new Bundle();
                    bundle.putString("tagString", tagString);
                    message.setData(bundle);
                    message.what = MESSAGE_TAG_CLICK;
                    tagClickListener.handler.sendMessage(message);
                }
                break;
            default:
                break;
        }
    }


    public String[] getStrings() {
        List<String> tagStringList = new ArrayList<String>();
        TextView textView;
        for (int i = 0; i < getChildCount(); i++) {
            textView = (TextView) getChildAt(i).findViewById(R.id.tag_text);
            tagStringList.add(textView.getText().toString());
        }
        String[] strs = new String[tagStringList.size()];
        strs = tagStringList.toArray(strs);

        return strs;
    }


    public void addClickListenner(MyClickListenner tagClickListener) {
        // TODO Auto-generated method stub
        this.tagClickListener = tagClickListener;

    }

}
